import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/home/widgets/QGLimitWidget.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGHomeCell extends StatefulWidget {
  Map? data;
  QGHomeCell({super.key, this.data});

  @override
  State<QGHomeCell> createState() => _QGHomeCellState();
}

class _QGHomeCellState extends State<QGHomeCell> {
  String? countdown;
  Timer? timer;
  String? ing;
  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  updateCountdown() {
    countdown = getCountdown(widget.data?["publish_time"]);
    if (countdown != null) {
      timer?.cancel();
      timer = Timer.periodic(const Duration(seconds: 1), (timer) {
        countdown = getCountdown(widget.data?["publish_time"]);
        if (countdown == null) {
          timer.cancel();
        }
        if (mounted) {
          setState(() {});
        }
      });
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    if (inReview()) {
      widget.data?["price"] = "888";
    }

    updateCountdown();
    return Container(
      margin: EdgeInsets.fromLTRB(15.rpx, 0, 15.rpx, 20.rpx),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(10.rpx)),
      child: Stack(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(10.rpx),
            child: AspectRatio(
              aspectRatio:1,
              child:ImageFiltered(
                 imageFilter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
                child: QGImage(url: widget.data?["thumb_img"]),
              ),
            ),
          ),
            Positioned(
              left: 0,
              right:0,
              bottom: 0,
              top: 0,
              child:Align(
                alignment: Alignment.center,
                child: QGImage(url: widget.data?["thumb_img"],width: 230.rpx,height: 230.rpx),
              )
               ,
            ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 15.rpx,vertical: 10.rpx),
              color: Color.fromARGB(128, 0, 0, 0), // ARGB格式，A为透明度（0-255），这里0.5的透明度
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                   Text(
                        widget.data?["name"] ?? "",
                        style: TextStyle(
                            color: const Color(0xFFffffff),
                            fontSize: 15.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                      SizedBox(height: 5.rpx,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          QGLimitWidget(num: widget.data?["total"] ?? "",dnum:widget.data?["circulation_amount"]??""),
                          Expanded(child: SizedBox()),
                           Text(
                        '￥',
                          style: TextStyle(
                              color: const Color(0xFFffffff),
                              fontSize: 20.rpx,
                              fontWeight: FontWeight.bold),
                        ),
                         Text(
                        '${widget.data?["price"]}',
                        style: TextStyle(
                            color: const Color(0xFFffffff),
                            fontSize: 30.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                        ],
                      )
                ],
              ),
            ),
          ),
          

          // Container(
          //   color: Color(0XFFffffff),
          //   padding: EdgeInsets.only(bottom: 10.rpx),
          //   child: Column(
          //     children: [
          //       Stack(r
          //         children: [
          //           ClipRRect(
          //             borderRadius: BorderRadius.circular(10.rpx),
          //             child: AspectRatio(
          //               aspectRatio: 345 / 345,
          //               child: QGImage(url: widget.data?["thumb_img"]),
          //             ),
          //           ),
          //         ],
          //       ),
          //       SizedBox(height: 18.rpx),
          //       Row(
          //         mainAxisAlignment: MainAxisAlignment.start,
          //         children: [
          //           SizedBox(width: 20.rpx),
          //           Flexible(
          //             child: Text(
          //               widget.data?["name"] ?? "",
          //               style: TextStyle(
          //                   color: const Color(0xFF111111),
          //                   fontSize: 16.rpx,
          //                   fontWeight: FontWeight.bold),
          //             ),
          //           ),
          //           SizedBox(width: 10.rpx),
          //           QGLimitWidget(num: widget.data?["total"] ?? ""),
          //           SizedBox(width: 10.rpx),
          //         ],
          //       ),
          //       SizedBox(height: 18.rpx),
          //       Row(
          //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //         children: [
          //           Expanded(
          //             child: Row(
          //               children: [
          //                 SizedBox(width: 20.rpx),
          //                 ClipRRect(
          //                   borderRadius: BorderRadius.circular(12.rpx),
          //                   child: QGImage(
          //                       url: widget.data?["artist_avatar"],
          //                       width: 24.rpx,
          //                       height: 24.rpx),
          //                 ),
          //                 SizedBox(width: 6.rpx),
          //                 Expanded(
          //                   child: Text(
          //                     widget.data?["artist_name"] ?? "",
          //                     style: TextStyle(
          //                         color: const Color.fromRGBO(17, 17, 17, 0.6),
          //                         fontSize: 12.rpx,
          //                         fontWeight: FontWeight.normal),
          //                   ),
          //                 ),
          //               ],
          //             ),
          //           ),
          //           Container(
          //             margin: EdgeInsets.only(right: 26.rpx),
          //             child: Text(
          //               '￥${widget.data?["price"]}',
          //               style: TextStyle(
          //                   color: const Color(0xFF111111),
          //                   fontSize: 22.rpx,
          //                   fontWeight: FontWeight.bold),
          //             ),
          //           ),
          //         ],
          //       )
          //     ],
          //   ),
          // ),
          buildSoldout(),
          buildCountdown(),
        ],
      ),
    );
  }

  buildState(state) {
    return Positioned(
        top: 16.rpx,
        left: 16.rpx,
        child: ClipRRect(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 12.rpx),
            height: 34.rpx,
            decoration: BoxDecoration(
                gradient: const LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [Color(0xFFFFF25A), Color(0xFFDCFF72)],
                ),
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10.rpx),
                    topRight: Radius.circular(4.rpx),
                    bottomRight: Radius.circular(10.rpx),
                    bottomLeft: Radius.circular(4.rpx))),
            child: Row(children: [
              Text(
                state ?? "",
                style: TextStyle(
                    color: const Color(0xFF111111),
                    fontSize: 12.rpx,
                    fontWeight: FontWeight.normal),
              ),
            ]),
          ),
        ));
  }

  buildCountdown() {
    if (inReview()) {
      return Container();
    }

    if (countdown == null) {
      return Container();
    }
    return buildState("即将开售：$countdown");
  }

  buildSoldout() {
    if (inReview()) {
      return Container();
    }

    if (widget.data?["isSoldOut"] != 1) {
      if (countdown == null) {
        return buildState("进行中");
      }

      return Container();
    }

    return Positioned(
        top: 16.rpx,
        left: 16.rpx,
        child: ClipRRect(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10.rpx),
              topRight: Radius.circular(4.rpx),
              bottomRight: Radius.circular(10.rpx),
              bottomLeft: Radius.circular(4.rpx)),
          child: Container(
            width: 80.rpx,
            height: 34.rpx,
            color: const Color.fromRGBO(0, 0, 0, 0.5),
            child: Row(children: [
              SizedBox(width: 12.rpx),
              QGImage(asset: "home_soldout"),
              SizedBox(width: 5.rpx),
              Text(
                '已售罄',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 12.rpx,
                    fontWeight: FontWeight.normal),
              ),
            ]),
          ),
        ));
  }
}
